using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using CoreLib.Input;
using CoreLib.Plugins;
using System.Resources;
using System.Reflection;
using System.Drawing;
namespace CoreLib
{
    public class ThirdPersonCamera:CameraPlugin
    {
         float sngPositionX;
    float PositionY;
    float PositionZ;
    float lookatX;
    float lookatY;
    float lookatZ;
    float AngleX;
    float AngleY;

    float sngWalk;
    float sngStrafe;
    ResourceManager rm;
        public enum eType
        {
            ThirdPerson,
            ChaseCamera,
        }
        public static eType Type;
        public static Keys ForwardKey = Keys.W;
        public static Keys BackwardKey = Keys.S;
        public static Keys LeftTurnKey = Keys.A;
        public static Keys RightTurnKey = Keys.D;

        public ThirdPersonCamera(Game game)
            : base(game) 
        {
            PluginName = "FirstPersonCamera";
            PluginDescription = "A camera with a first person perspective. It features predefined action keys for move and rotate";

            PluginImage = (Bitmap)Image.FromFile(System.Environment.CurrentDirectory + "/plugins/firstcamerabutton.png");

        }

        public  override void Initialize()
        {
          

        }
        public void SetCamera(Vector3 CameraPos, Vector3 CameraLookAt)
        {

             Engine.Viewport = Engine.graphics.GraphicsDevice.Viewport;
             AspectRatio = (float) Engine.Viewport.Width / (float) Engine.Viewport.Height;
               Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                                                                        AspectRatio,
                                                                      10, 10000);
            //view = Matrix.CreateLookAt(new Vector3(4000, 0, 0),
                  //                           Vector3.Zero,
                   //                          Vector3.Up);

            Vector3 n = Vector3.Zero;
            Vector3 u = Vector3.Up;

            View = Matrix.CreateLookAt( CameraPos,  CameraLookAt,
                                 u);
            PositionVector3 = CameraPos;
            World = PositionMatrix * RotationMatrix;

        }

        public override void Update(GameTime gameTime)
        {


                        HandleInput();
                        CheckMovement();
                       SetCamera(new Vector3(sngPositionX, PositionY, PositionZ), new Vector3(lookatX, lookatY, lookatZ));
                
              
        }
      void HandleInput()
      {
        if (KeyBoard.IsKeyDown(ForwardKey))
            sngWalk = 5;
        if (KeyBoard.IsKeyDown(BackwardKey))
            sngWalk = -5;
        if (KeyBoard.IsKeyDown(LeftTurnKey))
           AngleY = AngleY - TurnSpeed;
        if (KeyBoard.IsKeyDown(RightTurnKey))
            AngleY = AngleY + TurnSpeed;
        if (KeyBoard.IsKeyDown(Keys.Up))
            AngleX = AngleX - TurnSpeed;
        if (KeyBoard.IsKeyDown(Keys.Down))
            AngleX = AngleX + TurnSpeed;
        
      }
        void CheckMovement()
        {
        if( AngleX > 1.3)
        AngleX = 1.3f;
        if( AngleX < -1.3f)
        AngleX = -1.3f;   
            if (sngWalk > 0)
            {
                sngWalk = sngWalk -Speed;
                if (sngWalk < 0 )
                    sngWalk = 0;
            }
           if(sngWalk < 0)
               {
                   sngWalk = sngWalk + Speed;
                if (sngWalk > 0)
                    sngWalk = 0;
                }
        
        

  
          if(sngStrafe> 0)
                sngStrafe = sngStrafe - 0.005f;
                if (sngStrafe < 0 ) sngStrafe = 0;
            if( sngStrafe< 0)
                sngStrafe = sngStrafe + 0.005f;
                if (sngStrafe > 0) sngStrafe = 0;


        sngPositionX = sngPositionX + ((float)System.Math.Cos(AngleY) * sngWalk / 5) + ((float)System.Math.Cos(AngleY + 3.141596f / 2) * sngStrafe / 5);
        PositionZ = PositionZ + ((float)System.Math.Sin(AngleY) * sngWalk / 5) + ((float)System.Math.Sin(AngleY + 3.141596f / 2) * sngStrafe / 5);



        lookatX = sngPositionX + (float)System.Math.Cos(AngleY);
        lookatY = PositionY + (float)System.Math.Tan(AngleX);
        lookatZ = PositionZ + (float)System.Math.Sin(AngleY);
        sngWalk = 0;

        }
     
    }
}
